package org.example.clouddemo.user.controller.admin;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import org.example.clouddemo.bean.common.entity.User;
import org.example.clouddemo.user.service.common.UserService;
import org.example.common.response.ApiResult;
import org.example.common.response.ApiResultCode;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;


/**
 * 用户表 admin接口
 */
@RequiredArgsConstructor
@RestController("adminUserController")
@RequestMapping("/api/admin/user")
public class UserController {

    private final UserService userService;


    /**
     * 多页查询
     * @param page 页数
     * @param pageSize 页面大小
     * @param user 用户对象
     * @return 查询成功信息
     */
    @PostMapping("list/{page}/{pageSize}") // 使用路径参数
    public ApiResult<IPage<User>> list(@PathVariable("page") int page, @PathVariable("pageSize") int pageSize, @RequestBody User user) {
        Page<User> pageInfo = new Page<>();
        pageInfo.setCurrent(page);
        pageInfo.setSize(pageSize);
        IPage<User> result = userService.selectPageVo(pageInfo, user);
        return ApiResult.success(result);
    }

    /**
     * 单条用户查询
     * @param id 用户id
     * @return 返回执行结果
     */
    @PostMapping("queryOne")
    public ApiResult<User> queryOne(@RequestBody Long id) { // 携带请求体
        return ApiResult.success(userService.selectOne(id));
    }


    /**
     * 基于用户id删除用户
     * @param id 用户id
     * @return 返回执行结果
     */
    @PostMapping("delete/{id}")
    public ApiResult<Integer> delete(@PathVariable("id") String id) {
        return ApiResult.success(userService.delete(id));
    }

    /**
     * 更新用户信息
     * @param user 用户对象
     * @return 返回执行结果
     */
    @PostMapping("update")
    public ApiResult<Integer> update(@RequestBody User user) {
        if (StringUtils.isEmpty(user.getId())) {
            return ApiResult.res(ApiResultCode.PARAMS_NOT_EXIST);
        }
        return ApiResult.success(userService.update(user));
    }

}
